package com.adee.gulimall.search.controller;

import com.adee.gulimall.util.exception.BizCodeEnum;
import com.adee.gulimall.common.to.es.SkuESModel;
import com.adee.gulimall.util.utils.R;
import com.adee.gulimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSaveController {

    @Autowired
    ProductSaveService productSaveService;

    // 上架商品
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuESModel> skuESModels) {
        try {
            boolean success = productSaveService.productStatusUp(skuESModels);
            if(!success) {
                log.error("ElasticSaveController商品上架失败");
                return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION);
            }
        } catch (IOException e) {
            log.error("ElasticSaveController商品上架异常：{}", e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION);
        }
        return R.ok();
    }
}
